<template>
    <div>
        <div class="search">
            <input v-model="keyword" class="search-input" type="text" placeholder="请输入城市名称">
        </div>
        <div
            class="search-conent"
            v-show="keyword"
            ref="list"
        >
            <ul>
                <li
                    class="search-item border-bottom"
                    v-for="item of list"
                    :key="item.id"
                    @click="changeCitya(item.name)"
                >
                    {{item.name}}
                </li>
                <li class="search-item border-bottom" v-show="showDate">
                    暂无搜索结果
                </li>
            </ul>
        </div>
    </div>
</template>

<script>
/* eslint-disable */
import { mapMutations } from 'vuex'
import BScroll from 'better-scroll'
export default {
    name: 'citySearch',
    props: {
        cities : Object
    },
    data () {
        return {
            keyword: '',
            list: [],
            timer: null
        }
    },
    computed: {
        showDate() {
            return !this.list.length
        }
    },
    mounted() {
        this.scroll = new BScroll(this.$refs.list)
    },
    methods: {
        changeCitya(city) {
            this.changeCity(city)
            this.$router.push('/')
        },
        ...mapMutations(['changeCity'])
    },
    watch: {
        keyword () {
            if(this.timer){
                clearTimeout(this.timer)
            }
            this.timer = setTimeout(()=>{
                const result = []
                for (let i in this.cities){
                    this.cities[i].forEach((v) => {
                        if(v.name.indexOf(this.keyword) > -1 || v.spell.indexOf(this.keyword) > -1){
                            result.push(v)
                        }
                    });
                }
                return this.list = result
            },100)
        }
    }
}
</script>

<style lang="stylus" scoped>
    @import '~styles/varibles.styl';
    .search
        height .76rem
        line-height .76rem
        padding 0 .1rem
        background $bgColor
        .search-input
            box-sizing border-box
            width 100%
            height .62rem
            line-height .62rem
            padding 0 .1rem
            text-align center
            color #666666
            border-radius .06rem
    .search-conent
        position absolute
        overflow hidden
        top 1.62rem
        left 0
        right 0
        bottom 0
        background #ddd
        z-index 1
        .search-item
            line-height .62rem
            padding-left .2rem
            background #ffffff
            color #666666
</style>
